package com.aplink.generic.constant.type;

import java.text.ParseException;

import com.aplink.generic.util.DateTimeUtils;

public enum SqlDataType {
    CHAR("Char"),
    DATETIME("Datetime"),
    DOUBLE("Double"),
    INTEGER("Integer"),
    LONG("Long");
    public static SqlDataType get(final Object value) {
        if (value instanceof Double) {
            return DOUBLE;
        } else if (value instanceof Long) {
            return LONG;
        }
        return CHAR;
    }

    /**
     * @param value
     * @return return the datatype matches with the parameter, return null if
     *         not found.
     */
    public static SqlDataType get(final String value) {
        for (final SqlDataType dataType : SqlDataType.values()) {
            if (dataType.value.equalsIgnoreCase(value)) {
                return dataType;
            }
        }
        return CHAR;
    }

    private String value;

    private SqlDataType(final String value) {
        this.value = value;
    }

    public String getValue() {
        return this.value;
    }

    public Object parse(final String value) {
        if (equals(INTEGER)) {
            return Integer.parseInt(value);
        } else if (equals(DATETIME)) {
            try {
                return DateTimeUtils.getDateTime(Long.parseLong(value), null);
            } catch (final ParseException e) {
                e.printStackTrace();
            }
        } else if (equals(LONG)) {
            return Long.parseLong(value);
        } else if (equals(CHAR)) {
            return value;
        } else if (equals(DOUBLE)) {
            return Double.parseDouble(value);
        }
        return null;
    }
}
